package com.ss.android.article.base.feature.subscribe.b;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.bytedance.common.utility.collection.WeakList;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;
import com.ss.android.article.base.feature.subscribe.model.SubscribeData;
import com.ss.android.common.util.NetworkUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public final class e implements Callback {
    protected Handler a;
    WeakList subscribeDataChangeListenerList;
    protected Context c;
    public boolean d;
    public boolean e;
    long f;
    int g;
    public List h;
    public List i;
    String j;
    public boolean k;
    public boolean l;
    private boolean m;

    public e(Context arg4) {
        this.d = false;
        this.e = false;
        this.f = 0;
        this.g = 0;
        this.h = new ArrayList();
        this.i = new ArrayList();
        this.j = null;
        this.k = false;
        this.l = false;
        this.m = false;
        this.c = arg4.getApplicationContext();
        this.a = new WeakHandler(((Callback)this));
        this.subscribeDataChangeListenerList = new WeakList();
    }

    public final void a(long arg4, boolean arg6) {
        if(arg4 > 0) {
            EntryItem v0 = EntryItem.optObtain(arg4);
            if(v0 != null && v0.isSubscribed() == arg6) {
                return;
            }

            if(!NetworkUtils.isNetworkAvailable(this.c)) {
                return;
            }

            new g(this, arg4).start();
        }
    }

    public final void a() {
        new com.ss.android.article.base.feature.subscribe.b.a(this.c, this.a).start();
    }

    public final void a(EntryItem arg9) {
        SubscribeData v0_2;
        Iterator v3;
        if(arg9 != null) {
            boolean v0 = arg9.isSubscribed();
            if(!v0) {
                v3 = this.i.iterator();
                int v0_3;
                do {
                    if(v3.hasNext()) {
                        if(arg9.mId != ((com.ss.android.article.base.feature.subscribe.model.d)v3.next()).b) {
                            continue;
                        }

                        v3.remove();
                        v0_3 = 1;
                    } else {
                        v0_3 = 0;
                    }

                    if(v0_3 == 0) {
                        return;
                    }

                    v0_2 = SubscribeData.create(0);
                    v0_2.c = arg9;
                    this.notifySubscribeDataChanged(v0_2);
                    v3 = this.i.iterator();
                    do {
                        if(v3.hasNext()) {
                            if(!((com.ss.android.article.base.feature.subscribe.model.d)v3.next()).f) {
                                continue;
                            }
                            v0_3 = 1;
                        } else {
                            v0_3 = 0;
                        }
                        break;
                    }
                    while(true);
                    if(v0_3 != 0) {
                        return;
                    }
                    this.l = false;
                    return;
                } while(true);
            } else if(!arg9.isIdOnly()) {
                v3 = this.i.iterator();
                do {
                    if(v3.hasNext()) {
                        if(((com.ss.android.article.base.feature.subscribe.model.d)v3.next()).b != arg9.mId) {
                            continue;
                        }
                        return;
                    }
                    else {
                        com.ss.android.article.base.feature.subscribe.model.d v0_1 = new com.ss.android.article.base.feature.subscribe.model.d(arg9);
                        v0_1.f = true;
                        v0_1.itemDescription = arg9.mDescription;
                        this.i.add(0, v0_1);
                        v0_2 = SubscribeData.create(0);
                        v0_2.d = 1;
                        v0_2.c = arg9;
                        this.notifySubscribeDataChanged(v0_2);
                        this.l = true;
                        return;
                    }
                } while(true);
            } else {
                long v2 = arg9.mId;
                if(NetworkUtils.isNetworkAvailable(this.c)) {
                    new h(this, v2, v0).start();
                }
            }
        }
    }

    public final void a(int arg9) {
        ++this.g;
        boolean v6 = !this.m ? true : false;
        if(!this.m) {
            this.m = true;
        }

        this.d = true;
        new com.ss.android.article.base.feature.subscribe.b.b(this.c, this.a, this.g, arg9, this.j, v6, this.k).start();
    }

    final void a(long arg6) {
        if(!this.d) {
            long v0 = System.currentTimeMillis();
            if((this.m) && v0 - this.f <= arg6) {
                return;
            }

            if((this.m) && !NetworkUtils.isNetworkAvailable(this.c)) {
                return;
            }

            this.a(0);
        }
    }

    final void notifySubscribeDataChanged(SubscribeData subscribeData) {
        Iterator iterator = this.subscribeDataChangeListenerList.iterator();
        while(iterator.hasNext()) {
            Object v0 = iterator.next();
            if(v0 == null) {
                continue;
            }
            ((SubscribeManager.SubscribeDataChangeListener)v0).onSubscribeDataChanged(subscribeData);
        }
    }

    public final void b() {
        if((this.l) || (this.k)) {
            this.k = false;
            this.l = false;
        }
    }

    public final void handleMsg(Message message) {
        Iterator v5;
        SubscribeData v0_2;
        Object v0;
        int v6 = 2;
        switch(message.what) {
            case 1: {
                if(message.arg1 == 0) {
                    v0 = message.obj;
                    this.h.clear();
                    this.h.addAll(((i)v0).c);
                    if(!this.h.isEmpty() && message.arg2 == v6) {
                        AppData appData = AppData.getInstance();
                        appData.mLastSyncEntryGroupListTime = System.currentTimeMillis();
                        SharedPreferences.Editor editor = appData.mContext.getSharedPreferences("app_setting", 0).edit();
                        editor.putLong("last_sync_entry_group_list_time", appData.mLastSyncEntryGroupListTime);
                        SharedPreferenceCompat.commit(editor);
                        Logger.v("AppData", "updateSyncEntryGroupList");
                    }
                }

                v0_2 = new SubscribeData();
                v0_2.a = v6;
                this.notifySubscribeDataChanged(v0_2);
                break;
            }
            case 2: {
                this.notifySubscribeDataChanged((SubscribeData) message.obj);
                break;
            }
            case 4: {
                v0 = null;
                Object v3 = (message.obj instanceof j) ? message.obj : v0;
                if(v3 == null) {
                    return;
                }

                if(((j)v3).a != this.g) {
                    return;
                }

                if(!((j)v3).h) {
                    this.d = false;
                }

                if(message.arg1 > 0) {
                    this.notifySubscribeDataChanged(SubscribeData.create(message.arg1));
                    return;
                }

                this.e = true;
                if(!((j)v3).h) {
                    this.f = System.currentTimeMillis();
                }

                if((((j)v3).k) || (((j)v3).h)) {
                    HashMap v4 = new HashMap();
                    if(((j)v3).i != null) {
                        v5 = this.i.iterator();
                        while(v5.hasNext()) {
                            v0 = v5.next();
                            if(!((com.ss.android.article.base.feature.subscribe.model.d)v0).f) {
                                continue;
                            }

                            v4.put(String.valueOf(((com.ss.android.article.base.feature.subscribe.model.d)v0).b), Boolean.TRUE);
                        }
                    }

                    this.i.clear();
                    this.j = ((j)v3).j;
                    if(((j)v3).i != null) {
                        v5 = ((j)v3).i.iterator();
                        while(v5.hasNext()) {
                            v0 = v5.next();
                            if(!v4.containsKey(String.valueOf(((com.ss.android.article.base.feature.subscribe.model.d)v0).b))) {
                                continue;
                            }

                            ((com.ss.android.article.base.feature.subscribe.model.d)v0).f = true;
                        }

                        this.i.addAll(((j)v3).i);
                    }

                    this.notifySubscribeDataChanged(SubscribeData.create(0));
                }
                else {
                    v0_2 = new SubscribeData();
                    v0_2.a = 5;
                    this.notifySubscribeDataChanged(v0_2);
                }

                boolean v0_3 = !((j)v3).l || (android.support.design.a.isTextEquals(this.j, ((j)v3).j)) ? false : true;
                if(this.k == v0_3) {
                    return;
                }

                this.k = v0_3;
                break;
            }
            case 5: {
                if(!(message.obj instanceof EntryItem)) {
                    return;
                }

                this.a((EntryItem) message.obj);
                break;
            }
        }
    }
}

